package com.meal.test.returntest;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Title: Result
 * @Description: 返回结果
 * @Author yangzhanghui
 * @Date 2020/5/517:27
 * @Version v1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Accessors(chain = true)
public class Result<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    public Result(ResultCode resultCode, T data){
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
    }

    public Result setResultCode(ResultCode resultCode){
        this.code = resultCode.code();
        this.message = resultCode.message();
        return this;
    }

    //返回成功
    public static Result success(){
        return new Result().setResultCode(ResultCode.SUCCESS);
    }

    //返回成功
    public static Result success(Object data){
        return new Result().setResultCode(ResultCode.SUCCESS).setData(data);
    }

    //返回失败
    public static Result failure(ResultCode resultCode){
        return new Result().setResultCode(resultCode);
    }

    //返回失败
    public static Result failure(ResultCode resultCode, Object data){
        return new Result().setResultCode(resultCode).setData(data);
    }
}
